<?php

namespace app\api\controller;

use app\common\library\wechat\WxPay;
use app\common\library\wxpay\FzweixinPay;
use think\Request;
use think\Response;
use think\facade\Db;

class Room extends Wap
{


    /**
     * 包厢
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {

            $post = $request->param();

            $order = array();
            $cleaning = array();


            $data = Db::name('customer_course')->where('store_id', $post['store_id'])->where('mark', 1)->select()->toArray();

            if (empty($data)) {
                $arr['code'] = 101;
                $arr['message'] = '未配置包间';
                Response::create($arr, 'json')->send();
                exit();
            }
            if (count($data) > 0) {
                foreach ($data as $i => $v) {
                    $coursr_id[] = $v['id'];
                }


                $list = Db::name('customer_course_package_id')->alias('m')->join('customer_package p', 'm.package_id=p.id')->where('p.type', '1')->where('m.course_id', 'in', $coursr_id)->group('m.course_id')->order('p.price asc')->select();

                if (count($list) > 0) {
                    foreach ($list as $i => $v) {
                        $coursr_id[] = $v['course_id'];
                    }
                }

            }

            if (isset($post['add_time'])) {
                if ($post['add_time'] != "") {
                    $add_hour = strtotime($post['add_time']);
                } else {
                    $add_hour = time();
                }

            } else {
                $add_hour = time();
            }


            if (count($coursr_id) > 0) {
                $order = Db::name('customer_order')->where('mark', 1)->where('seat_id', 'in', $coursr_id)->where('status', 'in', [0, 1, 2])->select();

//有未完成保洁订单
                //  $cleaning = Db::name('customer_order_cleaning')->where('seat_id', 'in', $coursr_id)->where('end_time', '>', time())->where('status', '<>', 4)->select();
                $cleaning = Db::name('customer_order_cleaning')->where('seat_id', 'in', $coursr_id)->where('status', '<>', 4)->select();


            }
            $data = array();

            if (count($list) > 0) {
                foreach ($list as $i => $v) {

                    $course = Db::name('customer_course')->where('id', $v['course_id'])->where('status', 1)->where('mark', 1)->find();

                    if ($course) {
                        cache('course' . $v['course_id'], false);
                        if (!cache('course' . $v['course_id'])) {
                            $course['labellist'] = $this->getlabel($course['id']);
                            $course['packagelist'] = $this->getpackage($course['id']);
                            if (isset($post['package_id'])) {
                                if ($post['package_id'] > 0) {
                                    $course['packageInfo'] = Db::name('customer_package')->where('id', $post['package_id'])->find();
                                } else {
                                    $course['packageInfo'] = $course['packagelist'][0];
                                }
                            } else {

                                $course['packageInfo'] = $course['packagelist'][0];
                            }
                            $course['package_name'] = array();
                            if (count($course['packagelist'])) {
                                foreach ($course['packagelist'] as $vale) {
                                    $course['package_name'][] = array(
                                        'name' => $vale['name'],
                                        'price' => $vale['price'] . '元/场',
                                    );
                                }
                            }
                            if (isset($course['image_url']) && $course['image_url'] != "") {
                                if ($course['image_url'] != "") {
                                    $imgsList = unserialize($course['image_url']);
                                    foreach ($imgsList as &$val) {
                                        $val = get_image_url($val);
                                    }
                                    $course['image_url'] = $imgsList;
                                } else {
                                    $course['image_url'] = '';
                                }

                            } else {
                                $course['image_url'] = '';
                            }

                            cache('course' . $v['course_id'], $course);
                        } else {
                            $course = cache('course' . $v['course_id']);
                        }


                        $orderStatus = 0;
                        // 查询订单
                        $statusdata = array();
                        $end_times = 0;
                        if (count($order) > 0) {
                            foreach ($order as $k => $m) {

                                if ($v['course_id'])
                                    if ($m['seat_id'] == $v['course_id']) {

                                        $add_time = $m['add_time'];
                                        $end_time = $m['end_time'];
                                        $end_hour = $add_hour + $course['packageInfo']['time'] * 3600;
                                        //有订单但未开始
                                        if ($m['add_time'] > time()) {
                                            if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {

                                                $cleaningdata = Db::name('customer_order_cleaning')->where('order_on', '=', $m['order_on'])->where('end_time', '>', time())->find();
                                                //保洁结束时间
                                                if ($cleaningdata) {
                                                    $end_time = $cleaningdata['end_time'];
                                                }
                                                $da = $this->Packagetime($end_time, $v['course_id'], $course['packageInfo'] ['id']);
                                                $end_times = date('H:i', $da['end_time']);
                                                $statusdata[] = '1';
                                            } else {
                                                $statusdata[] = '0';
                                            }
                                        }
                                        //有订单在使用
                                        if ($m['add_time'] < time()) {

                                            if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                                                $end_times = date('H:i', $end_time);
                                                $statusdata[] = '2';
                                            } else {
                                                $statusdata[] = '0';
                                            }
                                        }
                                    }
                            }

                            if (in_array('1', $statusdata, TRUE)) {
                                $orderStatus = 3;
                            } else {
                                $orderStatus = 0;
                            }
                            if (in_array('2', $statusdata, TRUE)) {
                                $orderStatus = 3;
                            }
                            if (in_array('5', $statusdata, TRUE)) {
                                $orderStatus = 1;
                            }

                        }
                        if ($orderStatus == 0) {
                            //有未完成保洁订单
                            if (count($cleaning) > 0) {
                                $statusdata = array();
                                foreach ($cleaning as $k => $m) {
//该房间 有未完成保洁订单
                                    if ($m['seat_id'] == $v['course_id']) {


                                        $orderD = Db::name('customer_order')->where('mark', 1)->where('order_on', $m['order_on'])->find();

                                        if ($orderD) {
                                            //保洁时间到了
                                            if ($m['add_time'] < time()) {
                                                if ($m['lamp_id'] == 1) {
                                                    $end_times = '即将';
                                                    $orderStatus = 2;
                                                } else {
                                                    $end_times = '即将';
                                                    $orderStatus = 5;
                                                }

                                            } else {
                                                $add_time = $m['add_time'];
                                                $end_time = $m['end_time'];
                                                $end_hour = time() + $course['packageInfo'] ['time'] * 3600;
                                                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                                                    $da = $this->Packagetime($end_time, $v['course_id'], $course['packageInfo'] ['id']);
                                                    $end_times = date('H:i', $da['end_time']);

                                                    $statusdata[] = '1';
                                                } else {
                                                    $statusdata[] = '0';
                                                }
                                            }


                                        }

                                    }

                                }


                            }
                            if (in_array('1', $statusdata, TRUE)) {
                                $orderStatus = 2;
                            }


                        }
                        if ($orderStatus == 0 && $course['subscribe'] == 1) {
                            $orderStatus = 1;
                        }

                        //  $data['time1'][] = array(
                        //                    'add_time' => $add_hour,
                        //                    'end_time' => $end_hour,
                        //                    'name' => $k,
                        //                    'status' => $status,
                        //                );
                        $str_end = [];
//                        for ($i = 0; $i < 24; $i++) {
//                            $orderif=[];
//
//                            if($i==0){
//                                $orderif=  Db::name('customer_order')->where('store_id', $post['store_id'])->where('add_time','<=',time()+$i * 3600)->where('end_time','>=',time()+$i * 3600)->where('seat_id', '=', $course['id'])->where('status', 'in', [0, 1, 2])->findOrEmpty();
//
//                            }else{
//                                $ti=  strtotime(date('Y-m-d H', time()).':00:00');
//                                $orderif=  Db::name('customer_order')->where('store_id', $post['store_id'])->where('add_time','<=',$ti+$i * 3600)->where('end_time','>=',$ti+$i * 3600)->where('seat_id', '=', $course['id'])->where('status', 'in', [0, 1, 2])->findOrEmpty();
//
//                            }
//
//                            $str_end[] = array(
//                                                'name' => date('H', time()+$i * 3600),
//                                                'status' =>empty($orderif)?0:1,
//                                            );
//
//                        }

                        $data[] = array(
                            'id' => $course['id'],
                            'name' => $course['name'],
                            'logo_url' => $course['image_url'],
                            'lable' => $course['labellist'],
                            'status' => $orderStatus,
                            'package_name' => $course['package_name'],
                            'end_time' => $end_times,
                            //  'str_end' => $str_end,
                        );
                    }


                }
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getpackage($userId)
    {
        $roleList = Db::name('customer_course_package_id')->alias("ur")
            ->field('r.name,r.price,r.time,r.id,r.rank')
            ->join('customer_package r', 'ur.package_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->where('r.type', '=', 1)
            ->order('r.rank asc')
            ->order('r.price asc')
            ->select()->toArray();
        return $roleList;
    }

    public function getpackages($userId)
    {
        $roleList = Db::name('customer_course_package_id')->alias("ur")
            ->field('r.name,r.price,r.time,r.id')
            ->join('customer_package r', 'ur.package_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->where('r.type', '=', 3)
            ->order('r.price asc')
            ->select()->toArray();
        return $roleList;
    }

    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getlabel($userId)
    {
        $roleList = Db::name('customer_course_label')->alias("ur")
            ->field('r.*')
            ->join('mini_lable r', 'ur.lable_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->select()->toArray();
        return $roleList;
    }

    /**
     * 套餐列表
     * @param Request $request
     * @return void
     */
    public function Package(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $data = array();
            $list = Db::name('customer_package')->where('mark', 1)->where('type', 1)->where('status', 1)->where('store_id', $post['store_id'])->select();
            foreach ($list as $i => $v) {
                $data[] = array(
                    'id' => $v['id'],
                    'time' => $v['time'],
                    'image_url' => $v['image_url'],
                    'name' => $v['name'],
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 包厢套餐
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function PackageCourse(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $data = array();
            if (isset($post['add_time'])) {
                if ($post['add_time'] != "") {
                    $add_timess = date('Y-m-d H:i:s', strtotime($post['add_time']));
                    $add_times = date('Y-m-d ', strtotime($post['add_time']));
                } else {
                    $add_times = date('Y-m-d', time());
                }
            } else {
                $add_times = date('Y-m-d', time());
            }
            $Package = Db::name('customer_package')->where('id', '=', $post['package_id'])->find();
            $order = Db::name('customer_order')->where('mark', 1)->where('seat_id', '=', $post['id'])->order('end_time asc')->where('status', 'in', [0, 1, 2])->select();
            $da['add_time'] = 0;
            $da['end_time'] = 0;

            if (count($order) > 0) {
                foreach ($order as $i => $v) {
                    $add_hour = $v['add_time'];
                    $end_hour = $v['end_time'];
                    $cleaning = Db::name('customer_order_cleaning')->where('order_on', $v['order_on'])->where('add_time', '=', $v['end_time'])->where('status', '<>', 4)->find();
                    if ($cleaning) {
                        $end_hour = $cleaning['end_time'];
                    }
                    if ($this->is_time_cross($add_hour, $end_hour, strtotime($add_timess), strtotime($add_timess) + $Package['time'] * 3600)) {
                        $da['add_time'] = $add_hour;
                        $da['end_time'] = $end_hour;
                        if ($Package['rank'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '当前已有预约，不可预约';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }

                }
            } else {
                $cleaninglist = Db::name('customer_order_cleaning')->where('mark', 1)->where('end_time', '>', time())->where('status', '<', 4)->where('seat_id', '=', $post['id'])->select();

                foreach ($cleaninglist as $value) {
                    $valueadd_time = $value['add_time'];
                    $valueend_time = $value['end_time'];
                    $orderda = Db::name('customer_order')->where('order_on', $value['order_on'])->find();
                    if ($orderda) {
                        if ($this->is_time_cross($valueadd_time, $valueend_time, strtotime($add_timess), strtotime($add_timess) + $Package['time'] * 3600)) {
                            $da['add_time'] = $value['add_time'];
                            $da['end_time'] = $value['end_time'];

                        }
                    }
                }
            }

            $cleaninglist = Db::name('customer_order_cleaning')->where('mark', 1)->where('status', '<', 4)->where('seat_id', '=', $post['id'])->select();
            $k = 0;
            for ($i = 0; $i < 24; $i++) {
                $status = 0;
                $statusdata = array();
                $add_hour = strtotime($add_times) + $i * 3600;
                $end_hour = strtotime($add_times) + 3600 + $i * 3600;
                if ($add_hour < time()) {
                    $status = 1;
                } else {
                    if (count($order) > 0) {
                        foreach ($order as $m) {

                            $add_time = $m['add_time'];
                            $end_time = $m['end_time'];

                            if ($m['add_time'] > time()) {
                                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {

                                    $statusdata[] = '1';
                                } else {
                                    $statusdata[] = '0';
                                }
                            }
                            if ($m['add_time'] < time()) {
                                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                                    $statusdata[] = '1';
                                } else {
                                    $statusdata[] = '0';
                                }
                            }


                        }

                        if (in_array('1', $statusdata, TRUE)) {
                            $status = 1;
                        } else {
                            $status = 0;
                        }
                    }
                    if (count($cleaninglist) > 0) {
                        foreach ($cleaninglist as $value) {
                            if ($value['order_on']) {
                                $orderda = Db::name('customer_order')->where('order_on', $value['order_on'])->find();
                                if ($orderda) {
                                    $valueadd_time = $value['add_time'];
                                    $valueend_time = $value['end_time'];

                                    if ($this->is_time_cross($add_hour, $end_hour, $valueadd_time, $valueend_time)) {
                                        $statusdata[] = '1';
                                    } else {
                                        $statusdata[] = '0';
                                    }
                                }

                            }
                        }
                        if (in_array('1', $statusdata, TRUE)) {
                            $status = 1;
                        } else {
                            $status = 0;
                        }
                    }
                }
                $data['time'][] = array(
                    'add_time' => $add_hour,
                    'end_time' => $end_hour,
                    'name' => $k,
                    'status' => $status,
                );
                $k += 1;
            }

            if (isset($post['add_time'])) {
                if ($post['add_time'] != "") {
                    $add_time = $post['add_time'];
                } else {
                    $add_time = date('Y-m-d', time());
                }
            } else {
                $add_time = date('Y-m-d', time());
            }

            $k = 0;
            $add_times = date('Y-m-d', (strtotime($add_time) + 86400));

            for ($i = 0; $i < 24; $i++) {
                $status = 0;
                $statusdata = array();
                $add_hour = strtotime($add_times) + $i * 3600;
                $end_hour = strtotime($add_times) + 3600 + $i * 3600;

                if (count($order) > 0) {
                    foreach ($order as $o => $info) {
                        $add_time = $info['add_time'];
                        $end_time = $info['end_time'];
                        if ($info['add_time'] > time()) {
                            if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {

                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }
                        }
                        if ($info['add_time'] < time()) {
                            if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {

                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }
                        }
                    }


                    if (in_array('1', $statusdata, TRUE)) {
                        $status = 1;
                    } else {
                        $status = 0;
                    }

                }
                if (count($cleaninglist) > 0) {
                    foreach ($cleaninglist as $value) {
                        $valueadd_time = $value['add_time'];
                        $valueend_time = $value['end_time'];
                        $orderda = Db::name('customer_order')->where('order_on', $value['order_on'])->find();
                        if ($orderda) {


                            if ($this->is_time_cross($add_hour, $end_hour, $valueadd_time, $valueend_time)) {
                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }
                        }

                    }
                    if (in_array('1', $statusdata, TRUE)) {
                        $status = 1;
                    } else {
                        $status = 0;
                    }

                }
                $data['time1'][] = array(
                    'add_time' => $add_hour,
                    'end_time' => $end_hour,
                    'name' => $k,
                    'status' => $status,
                );
                $k += 1;
            }

            if ($da['end_time']) {
                $data['end_time'] = date('Y-m-d H:i:s', $da['end_time']);
            } else {
                $data['end_time'] = 0;
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }

    }

    /**
     *
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function shi(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (isset($post['add_time'])) {
                if ($post['add_time'] != "") {
                    $add_time = strtotime($post['add_time'] . '00');
                    $end_time = strtotime($post['end_time']);
                }
            }
            $da = array();
            for ($k = 0; $k < 60; $k++) {
                $add_time_fen = $add_time + $k * 60;
                if ($add_time_fen > $end_time) {
                    $da[] = array(
                        'name' => date('i', $add_time_fen),
                    );
                }

            }
            $arr['code'] = 200;
            $arr['data'] = $da;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 获取小时
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function hour(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (isset($post['add_time'])) {
                if ($post['add_time'] != "") {
                    $add_timess = strtotime($post['add_time']);
                    $add_times = strtotime($post['end_time']);
                }
            }
            $Package = Db::name('customer_package')->where('id', '=', $post['package_id'])->find();
            $order = Db::name('customer_order')->where('seat_id', '=', $post['id'])->where('status', 'in', [0, 1, 2])->select();
            $cleaning = Db::name('customer_order_cleaning')->where('seat_id', '=', $post['id'])->where('end_time', '>', $add_times)->select();

            $data = array();
            $k = 0;
            for ($i = 0; $i < 24; $i++) {
                $status = 0;
                $statusdata = array();
                $add_hour = ($add_timess) + $i * 3600;
                $end_hour = ($add_timess) + 3600 + $i * 3600;


                if ($end_hour > $add_times) {


                    if (count($order) > 0) {
                        foreach ($order as $m) {
                            $add_time = $m['add_time'];
                            $end_time = $m['end_time'];

                            if ($m['add_time'] > time()) {
                                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {

                                    $end_times = date('H:i', $end_time);
                                    $statusdata[] = '1';
                                } else {
                                    $statusdata[] = '0';
                                }
                            }
                            if ($m['add_time'] < time()) {
                                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                                    $statusdata[] = '1';
                                } else {
                                    $statusdata[] = '0';
                                }
                            }


                        }

                        if (in_array('1', $statusdata, TRUE)) {
                            $status = 1;
                        } else {
                            $status = 0;
                        }

                    }
                    if (count($cleaning) > 0) {


                        foreach ($cleaning as $value) {


                            $valueadd_time = $value['add_time'];
                            $valueend_time = $value['end_time'];
                            if ($this->is_time_cross($add_hour, $end_hour, $valueadd_time, $valueend_time)) {

                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }


                        }
                        if (in_array('1', $statusdata, TRUE)) {
                            $status = 1;
                        } else {
                            $status = 0;
                        }
                    }
                    $data['time'][] = array(
                        'name' => $k,

                    );
                }


                $k += 1;
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 套餐
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function PackageCourseTime(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $list = $this->getpackage($post['id']);


            $Package = Db::name('customer_package')->where('id', '=', $list[0]['id'])->find();
            $order = Db::name('customer_order')->where('mark', 1)->where('seat_id', '=', $post['id'])->where('mark', 1)->where('status', 'in', [0, 1, 2])->select();

            $da['add_time'] = 0;
            $da['end_time'] = 0;
            if (count($order) > 0) {
                foreach ($order as $i => $v) {
                    $add_hour = $v['add_time'];
                    $end_hour = $v['end_time'];
                    $cleaning = Db::name('customer_order_cleaning')->where('order_on', '=', $v['order_on'])->where('add_time', '=', $v['end_time'])->where('status', '<>', 4)->find();
                    if ($cleaning) {

                        $end_hour = $cleaning['end_time'];
                    }
                    if ($this->is_time_cross($add_hour, $end_hour, time(), time() + $Package['time'] * 60)) {
                        //  $da['add_time'] = $add_hour['end_time'];
                        //  $da['end_time'] = $end_hour['add_time'];
                        $da['add_time'] = $add_hour;
                        $da['end_time'] = $end_hour;
                    } else {
                        $statusdata[] = '0';
                    }
                }
            } else {
                $cleaninglist = Db::name('customer_order_cleaning')->where('mark', 1)->where('status', '<', 4)->where('end_time', '>', time())->where('seat_id', '=', $post['id'])->select();

                foreach ($cleaninglist as $value) {
                    $valueadd_time = $value['add_time'];
                    $valueend_time = $value['end_time'];

                    $order = Db::name('customer_order')->where('mark', 1)->where('order_on', $value['order_on'])->find();
                    if ($order) {
                        if ($this->is_time_cross($valueadd_time, $valueend_time, time(), time() + $Package['time'] * 60)) {
                            $da['add_time'] = $value['add_time'];
                            $da['end_time'] = $value['end_time'];

                        }
                    }

                }
            }

            foreach ($list as $i => $v) {
                $data['package'][] = array(
                    'name' => $v['name'],
                    'time' => $v['time'],
                    'rank' => $v['rank'],
                    'id' => $v['id'],
                    'price' => $v['price'] . '元/场',
                );
            }


            if ($da['end_time']) {
                $data['end_time'] = date('Y-m-d H:i:s', $da['end_time']);
            } else {
                $data['end_time'] = 0;
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 递归查询是否有交互
     * @param $add_time
     * @param $course_id
     * @param $Package_id
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function Packagetime($add_time, $course_id, $Package_id)
    {

        $Package = Db::name('customer_package')->where('id', '=', $Package_id)->find();
        $order = Db::name('customer_order')->where('seat_id', '=', $course_id)->where('mark', 1)->order('end_time asc')->where('status', 'in', [0, 1, 2])->select();


        $da['end_time'] = $add_time;

        foreach ($order as $i => $v) {
            $add_hour = $v['add_time'];
            $end_hour = $v['end_time'];

            if ($this->is_time_cross($add_hour, $end_hour, $add_time, $add_time + $Package['time'] * 3600)) {

                $da['add_time'] = $v['add_time'];
                $da['end_time'] = $v['end_time'];
                $this->Packagetime($da['end_time'], $course_id, $Package_id);
            } else {
                $da['add_time'] = $v['add_time'];
                $da['end_time'] = $v['end_time'];
            }
        }

        return $da;

    }


    /**
     * 查看是否预约
     * @param $store_id
     * @param $add_time
     * @param $end_time
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function repeat($store_id, $add_time, $end_time)
    {
        $order = Db::name('customer_order')->where('mark', 1)->where('seat_id', $store_id)->where('status', 'in', [0, 1, 2])->select();
        $status = array();
        if (count($order) > 0) {

            foreach ($order as $i => $v) {
                $add_hour = $v['add_time'];
                $end_hour = $v['end_time'];
                $cleaning = Db::name('customer_order_cleaning')->where('order_on', $v['order_on'])->where('add_time', $v['end_time'])->find();
                if ($cleaning) {
                    $end_hour = $cleaning['end_time'];
                }
                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                    $status[] = '1';
                } else {
                    $status[] = '0';
                }
            }
        } else {
            $order = Db::name('customer_order_cleaning')->where('seat_id', $store_id)->where('end_time', '>', time())->select();
            foreach ($order as $i => $v) {
                $add_hour = $v['add_time'];
                $end_hour = $v['end_time'];
                $order = Db::name('customer_order')->where('mark', 1)->where('order_on', $v['order_on'])->find();
                if ($order) {
                    if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                        $status[] = '1';
                    } else {
                        $status[] = '0';
                    }
                }

            }
        }
        if (in_array('1', $status, TRUE)) {
            $orderStatus = 1;
        } else {
            $orderStatus = 0;
        }
        return $orderStatus;
    }

    /**
     * 查看是否预约
     * @param $store_id
     * @param $add_time
     * @param $end_time
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function arepeat($store_id, $add_time, $end_time, $orderno)
    {
        $order1 = Db::name('customer_order')->where('mark', 1)->where('seat_id', $store_id)->where('status', 2)->find();
        if (!empty($order1)) {
            return 1;
        }
        $order = Db::name('customer_order')->where('mark', 1)->where('order_on', '<>', $orderno)->where('seat_id', $store_id)->where('status', 'in', [0, 1])->select()->toArray();
        $orderStatus = 0;
        if (!empty($order)) {
            foreach ($order as $i => $v) {
                if ($this->is_time_cross($v['add_time'], $v['end_time'], $add_time, $end_time)) {
                    $orderStatus = 1;
                }
            }
        }

        return $orderStatus;
    }

    /**
     * 获取成功
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function makeOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if ($user['status'] == 1) {
                $arr['code'] = 1001;
                $arr['message'] = '该账号被封禁';
                Response::create($arr, 'json')->send();
                exit();
            }
            $room = Db::name('mini_room_list')->where('id', $post['store_id'])->find();
            if (intval($room['time']) > 0) {
                $end_time = strtotime($post['end_time']) + $room['time'] * 60;
            } else {
                $end_time = strtotime($post['end_time']);
            }
            if (strtotime($post['add_time']) < time()) {
                $arr['code'] = 1001;
                $arr['message'] = '当前时间，不可预约';
                Response::create($arr, 'json')->send();
                exit();
            }
            // 查询当前是否预约
            $orderStatus = $this->repeat($post['seat_id'], strtotime($post['add_time']), $end_time);
            if ($orderStatus > 0) {
                $arr['code'] = 1001;
                $arr['message'] = '当前已有预约，不可重复预约';
                Response::create($arr, 'json')->send();
                exit();
            }
            $package = Db::name('customer_package')->where('mark', 1)->where('id', $post['package_id'])->find();
            $data = array(
                'order_on' => $this->orderOn(),
                'store_id' => $post['store_id'],
                'seat_id' => $post['seat_id'],
                'add_time' => strtotime($post['add_time']),
                'end_time' => strtotime($post['add_time']) + $package['time'] * 3600,
                'price' => $package['price'],
                'shi' => $package['time'],
                'user_id' => $user['id'],
                'create_time' => time(),
                'order_type' => 1,
                'package_id' => $post['package_id'],
            );
            // 查询是否重复

            if (Db::name('customer_order')->insert($data)) {
                $room = Db::name('mini_room_list')->where('id', $data['store_id'])->find();
                if (intval($room['time']) > 0) {

                    $userda = Db::name('customer_user')->where('mark', 1)->where('room_id', $data['store_id'])->where('is_cleaning', 1)->find();
                    if ($userda) {
                        $datac['order_on'] = $data['order_on'];
                        $datac['add_time'] = $data['end_time'];
                        $datac['end_time'] = $data['end_time'] + $room['time'] * 60;
                        $datac['seat_id'] = $post['seat_id'];
                        $datac['store_id'] = $post['store_id'];
                        $datac['user_id'] = $userda['id'];
                        Db::name('customer_order_cleaning')->insert($datac);
                    }

                }

                $da['order_on'] = $data['order_on'];
                $arr['code'] = 200;
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 预定详情
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function OrderDetail(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
            $info = Db::name('customer_course')->where('id', $order['seat_id'])->find();
            $package = Db::name('customer_package')->where('mark', 1)->where('id', $order['package_id'])->find();
            $store = Db::name('mini_room_list')->where('id', $info['store_id'])->find();
            if ($store['head_url'] != "") {
                $store['head_url'] = get_image_url($store['head_url']);
            }
            if ($store['sub_mch_id'] == null or $store['sub_mch_id'] == '') {
                $data['fz'] = 0;
            } else {
                $data['fz'] = 1;
            }
            $data['order'] = array(
                'order_on' => $order['order_on'],
                'price' => $order['price'],
                'name' => $info['name'],
                'store_name' => $store['room_name'],
                'add_time' => date('Y.m.d H:i:s', $order['add_time']) . '~~' . date('Y.m.d H:i:s', $order['end_time']),
                'package_name' => $package['name'],
                'head_url' => $store['head_url'],
            );
            //充值记录
            $recharge_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('store_id', $order['store_id'])->where('status', 1)->find();

            if ($recharge_order) {
                $data['user'] = array(
                    'balance' => $user['balance']
                );
            } else {
                $data['user'] = array(
                    'balance' => 0
                );
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 使用优惠券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function CouponOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();

            $userCoupon = Db::name('mini_user_coupon')->where('status', 0)->where('end_time', '>', time())->where('user_id', $user['id'])->select();
            $data = array();
            foreach ($userCoupon as $i => $v) {
                $couopnarea = Db::name('coupon_area')->where('store_id', $post['store_id'])->where('coupon_id', $v['coupon_id'])->find();
                if ($couopnarea) {
                    $coupon = Db::name('coupon')->where('id', $v['coupon_id'])->find();
                    $data[] = array(
                        'id' => $v['id'],
                        'price' => $v['price'],
                        'title' => $coupon['title'],
                        'add_time' => date('Y.m.d', $v['add_time']) . '-' . date('Y.m.d', $v['end_time']),
                        'hours_add_time' => $coupon['hours_add_time'],
                        'hours_end_time' => $coupon['hours_end_time'],
                        'store_name' => $this->getUserRoleList($v['coupon_id']),
                        'status' => $v['status']
                    );
                }
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    /**
     * 查询适用门店
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserRoleList($userId)
    {
        $roleList = Db::name('coupon_area')->alias("ur")
            ->field('r.room_name')
            ->join('mini_room_list r', 'ur.store_id=r.id')
            ->distinct(true)
            ->where('ur.coupon_id', '=', $userId)
            ->select()->toArray();
        return $roleList;
    }


    /**
     * 退订
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function makeUpdate(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();

            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('status', 1)->where('mark', 1)->where('order_on', $post['order_on'])->find();

            if (empty($order)) {
                $arr['code'] = 101;
                $arr['message'] = '该订单不可退订';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($order['add_time'] < time()) {
                $arr['code'] = 101;
                $arr['message'] = '进行中的订单不可退订';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($order['add_time'] > time()) {
                // 全额退款

                if ($order['pay_type'] == 1) {
                    // 微信退款
                    $wxapp = Db::name('mini_match')->where('id', 1)->find();

                    $wxConfig = array(
                        'app_id' => $wxapp['app_id'],
                        'app_key' => $wxapp['key'],
                        'mchid' => $wxapp['wc_hid'],
                        'app_secret' => $wxapp['api_key'],
                        'apikey' => $wxapp['api_key'],
                        'apiclient_url' => $wxapp['apiclient_url'],
                        'apiclient_key_url' => $wxapp['apiclient_key_url']
                    );

                    $WxPay = new WxPay($wxConfig);


                    if ($WxPay->refund($order['order_on'], $order['pay_price'], $order['pay_price']) == true) {

                    } else {
                        $arr['code'] = 101;
                        $arr['message'] = '退订失败，请联系管理员';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }


                $order['status'] = 3;
                $order['retreat_shi'] = $order['shi'];
                $order['retreat_price'] = $order['pay_price'] ? $order['pay_price'] : $order['balance'];
                $order['retreat_time'] = time();
                if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {

                    $orderlist = Db::name('customer_order')->where('status', 2)->where('add_time', '<', time())->where('end_time', '>', time())->select();
                    if (empty($orderlist)) {
                        $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                        $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
                        $door_list_data = $appkey;
                        $door_list_data['equipment_id'] = $door['door_id'];

                        if (true == $this->opens($door_list_data, 04)) {
                            // 记录信息
                        }
                    }

                    if ($order['pay_type'] == 2) {
                        $price = 0;
                        $user = Db::name('member')->where('id', $order['user_id'])->find();
                        $userBa['balance'] = $user['balance'] + ($order['balance'] - $price);
                        if (Db::name('member')->where('id', $order['user_id'])->update($userBa)) {
                            // 记录余额变动
                            $userbalance = array(
                                'price' => $order['balance'] - $price,
                                'type' => 1,
                                'status' => $order['order_on'],
                                'before_price' => $user['balance'],
                                'aater_price' => $userBa['balance'],
                                'create_time' => time(),
                                'user_id' => $user['id'],
                                'content' => "预约退订",
                            );
                            Db::name('customer_user_balance')->insert($userbalance);

                        }
                    }


                    // 查询是否有保洁
                    Db::name('customer_order_cleaning')->where('order_on', $order['order_on'])->delete();
                    if ($order['lamp_id'] == 1) {
                        $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                        if ($door) {
                            $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
                            $door_list_data = $appkey;
                            $door_list_data['equipment_id'] = $door['door_id'];
                            if ($door['door_id'] != "") {

                                switch ($door['door_type']) {
                                    case 0:
                                        $res = $this->opens($door_list_data, 04);
                                        if ($res == 'true') {
                                            // 记录信息
                                            $doorList = array(
                                                'admin_id' => $order['store_id'],
                                                'create_time' => time(),
                                                'user_id' => $order['user_id'],
                                                'status' => 1,
                                                'order_on' => $order['order_on'],
                                                'door_id' => $door['door_id'],
                                                'type' => 1,
                                            );
                                            Db::name('mini_admin_lamp_list')->insert($doorList);
                                        }
                                        break;
                                    case 1:
                                        $res = self::newopen($door['door_id'], 4);

                                        break;
                                    case 2:
                                        $res = self::mqttopenone($door['door_id'], 0);

                                        break;
                                    default:
                                        $res = $this->opens($door_list_data, 04);
                                        if ($res == 'true') {
                                            // 记录信息
                                            $doorList = array(
                                                'admin_id' => $order['store_id'],
                                                'create_time' => time(),
                                                'user_id' => $order['user_id'],
                                                'status' => 1,
                                                'order_on' => $order['order_on'],
                                                'door_id' => $door['door_id'],
                                                'type' => 1,
                                            );
                                            Db::name('mini_admin_lamp_list')->insert($doorList);
                                        }
                                        break;
                                }

                            }
                        }
                    }
                    $arr['code'] = 200;
                    $arr['message'] = '取消成功';
                    Response::create($arr, 'json')->send();
                    exit();
                }
            }

        }
    }


    /**
     * 退订
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function makeUpdatev1(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();

            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('status', 1)->where('mark', 1)->where('order_on', $post['order_on'])->find();

            if (empty($order)) {
                $arr['code'] = 101;
                $arr['message'] = '该订单不可退订';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($order['add_time'] < time()) {
                $arr['code'] = 101;
                $arr['message'] = '进行中的订单不可退订';
                Response::create($arr, 'json')->send();
                exit();
            }


            if ($order['add_time'] > time()) {
                // 全额退款

                if ($order['pay_type'] == 1) {
                    $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
                    $wxapp = Db::name('mini_match')->where('id', 1)->find();

                    $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $order['order_on'], $body = '', $total_fee = $order['pay_price'] * 100, $type = '', $room['sub_mch_id']);
                    if ($weixinpay->refund($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $order['order_on'])) {

                    } else {
                        $arr['code'] = 101;
                        $arr['message'] = '退订失败，请联系管理员';
                        Response::create($arr, 'json')->send();
                        exit();
                    }


                }


                $order['status'] = 3;
                $order['retreat_shi'] = $order['shi'];
                $order['retreat_price'] = $order['pay_price'] ? $order['pay_price'] : $order['balance'];
                $order['retreat_time'] = time();
                if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {
                    $orderlist = Db::name('customer_order')->where('status', 2)->where('add_time', '<', time())->where('end_time', '>', time())->select();
                    if (empty($orderlist)) {
                        $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                        $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
                        $door_list_data = $appkey;
                        $door_list_data['equipment_id'] = $door['door_id'];
                        switch ($door['door_type']) {
                            case 0:
                                $res = $this->opens($door_list_data, 04);
                                if ($res == 'true') {
                                    // 记录信息
                                    $doorList = array(
                                        'admin_id' => $order['store_id'],
                                        'create_time' => time(),
                                        'user_id' => $order['user_id'],
                                        'status' => 1,
                                        'order_on' => $order['order_on'],
                                        'door_id' => $door['door_id'],
                                        'type' => 1,
                                    );
                                    Db::name('mini_admin_lamp_list')->insert($doorList);
                                }
                                break;

                            case 1:
                                $res = self::newopen($door['door_id'], 4);

                                break;
                            case 2:
                                $res = self::mqttopenone($door['door_id'], 0);

                                break;
                            default:
                                $res = $this->opens($door_list_data, 04);
                                if ($res == 'true') {
                                    // 记录信息
                                    $doorList = array(
                                        'admin_id' => $order['store_id'],
                                        'create_time' => time(),
                                        'user_id' => $order['user_id'],
                                        'status' => 1,
                                        'order_on' => $order['order_on'],
                                        'door_id' => $door['door_id'],
                                        'type' => 1,
                                    );
                                    Db::name('mini_admin_lamp_list')->insert($doorList);
                                }
                                break;
                        }
                    }

                    if ($order['pay_type'] == 2) {
                        $price = 0;
                        $user = Db::name('member')->where('id', $order['user_id'])->find();
                        $userBa['balance'] = $user['balance'] + ($order['balance'] - $price);
                        if (Db::name('member')->where('id', $order['user_id'])->update($userBa)) {
                            // 记录余额变动
                            $userbalance = array(
                                'price' => $order['balance'] - $price,
                                'type' => 1,
                                'status' => $order['order_on'],
                                'before_price' => $user['balance'],
                                'aater_price' => $userBa['balance'],
                                'create_time' => time(),
                                'user_id' => $user['id'],
                                'content' => "预约退订",
                            );
                            Db::name('customer_user_balance')->insert($userbalance);

                        }
                    }

                    // 查询是否有保洁
                    Db::name('customer_order_cleaning')->where('order_on', $order['order_on'])->delete();
                    if ($order['lamp_id'] == 1) {
                        $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                        if ($door) {
                            $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
                            $door_list_data = $appkey;
                            $door_list_data['equipment_id'] = $door['door_id'];
                            if ($door['door_id'] != "") {

                                switch ($door['door_type']) {
                                    case 0:
                                        $res = $this->opens($door_list_data, 04);
                                        if ($res == 'true') {
                                            // 记录信息
                                            $doorList = array(
                                                'admin_id' => $order['store_id'],
                                                'create_time' => time(),
                                                'user_id' => $order['user_id'],
                                                'status' => 1,
                                                'order_on' => $order['order_on'],
                                                'door_id' => $door['door_id'],
                                                'type' => 1,
                                            );
                                            Db::name('mini_admin_lamp_list')->insert($doorList);
                                        }
                                        break;
                                    case 1:
                                        $res = self::newopen($door['door_id'], 4);

                                        break;
                                    case 2:
                                        $res = self::mqttopenone($door['door_id'], 0);

                                        break;
                                    default:
                                        $res = $this->opens($door_list_data, 04);
                                        if ($res == 'true') {
                                            // 记录信息
                                            $doorList = array(
                                                'admin_id' => $order['store_id'],
                                                'create_time' => time(),
                                                'user_id' => $order['user_id'],
                                                'status' => 1,
                                                'order_on' => $order['order_on'],
                                                'door_id' => $door['door_id'],
                                                'type' => 1,
                                            );
                                            Db::name('mini_admin_lamp_list')->insert($doorList);
                                        }
                                        break;
                                }

                            }
                        }
                    }
                    $arr['code'] = 200;
                    $arr['message'] = '取消成功';
                    Response::create($arr, 'json')->send();
                    exit();
                }
            }
        }
    }

    /**
     * 退订
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function amakeUpdate(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('status', 1)->where('mark', 1)->where('order_on', $post['order_on'])->find();
            if (empty($order)) {
                $arr['code'] = 101;
                $arr['message'] = '该订单不可提前开始';
                Response::create($arr, 'json')->send();
                exit();
            }

            // 查询当前是否预约
            $orderStatus = $this->arepeat($order['seat_id'], time(), time() + 3600 * $order['shi'], $post['order_on']);
            if ($orderStatus > 0) {
                $arr['code'] = 1001;
                $arr['message'] = '当前时间不可提前开始';
                Response::create($arr, 'json')->send();
                exit();
            }

            $stauts['status'] = 2;
            $stauts['update_time'] = time();
            $stauts['add_time'] = time();
            $stauts['end_time'] = time() + 3600 * $order['shi'];
            $res = Db::name('customer_order')->where('order_on', $post['order_on'])->update($stauts);
            if ($res) {
                $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
                $cleaning['add_time'] = $stauts['end_time'];
                $cleaning['end_time'] = $stauts['end_time'] + 60 * $room['time'];
                Db::name('customer_order_cleaning')->where('order_on', $post['order_on'])->update($cleaning);
                $arr['code'] = 200;
                $arr['message'] = '该订单提前成功';
                Response::create($arr, 'json')->send();
                exit();
            } else {
                $arr['code'] = 101;
                $arr['message'] = '该订单提前失败';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }

}